import unittest
from main.cpu import CPU
from main.ioDevs import IO
from main.process import PCB, Instruction, Process, ProcessStatus,IOInstruction
from main.shortTermSchedulers import Scheduler_ByPriorities
from main.dataStructures import ProcessesQueue
from main.memory import MMU_ContinuousAssignment,BestAdjustment,PhysicalMemory

class TestPlanners(unittest.TestCase):
    def setUp(self):
        self.memory = PhysicalMemory(5,BestAdjustment())
        self.mmu = MMU_ContinuousAssignment()
        self.ioQueue = IO()
        #
        self.pcb1 =  PCB(3)
        self.pcb2 =  PCB(6)
        self.pcb3 =  PCB(7)
        self.pcb4 =  PCB(7)
        #
        self.instruction1 =  IOInstruction(4,'Printer')
        self.instruction2 =  Instruction(ProcessStatus().IO,2)
        self.instruction3 =  Instruction(ProcessStatus().IO,3)
        self.instruction4 =  IOInstruction(4,'Printer')
        self.instruction5 =  Instruction(ProcessStatus().IO,5)
        self.instruction6 =  IOInstruction(4,'Printer')
        self.instruction7 =  Instruction(ProcessStatus().IO,7)
        self.instruction8 =  IOInstruction(4,'Printer')
        self.instruction9 =  Instruction(ProcessStatus().IO,9)
        self.instruction10=  Instruction(ProcessStatus().IO,10)
        self.instruction11=  Instruction(ProcessStatus().CPU,9)
        self.instruction12=  Instruction(ProcessStatus().CPU,10)
        #
        self.process1 = Process(self.pcb1)#io process
        self.process1.addInstruction(self.instruction1)
        self.process1.addInstruction(self.instruction2)
        self.process1.addInstruction(self.instruction3)
        self.process1.addInstruction(self.instruction4)
        #
        self.process2 = Process(self.pcb2)
        self.process2.addInstruction(self.instruction5)
        self.process2.addInstruction(self.instruction6)
        self.process2.addInstruction(self.instruction7)
        #
        self.process3 = Process(self.pcb3)
        self.process3.addInstruction(self.instruction8)
        self.process3.addInstruction(self.instruction9)
        self.process3.addInstruction(self.instruction10)
        #
        self.process4 = Process(self.pcb4)
        self.process4.addInstruction(self.instruction11)
        #
        self.process= ProcessesQueue()
        self.process.append(self.process1)
        self.process.append(self.process2)
        self.process.append(self.process3)
        #
        self.cpu = CPU(self.process, self.ioQueue,self.mmu,self.memory)
        
#--------------------------------------------------------------------------#
#------------------TESTING: Scheduler_ByPriorities-------------------------#
#--------------------------------------------------------------------------#
    def setUp_Scheduler_ByPriorities(self):
        self.planner = Scheduler_ByPriorities(self.process,self.cpu,self.ioQueue)        
        
    #Elige al proceso de mayor prioridad    
    def test_GetProcessWithBiggestPriority(self):
        self.setUp_Scheduler_ByPriorities()
        x = self.planner.getBiggestPriority()
        self.assertEquals(x, self.process1)
     
    #Elige al proceso de mayor prioridad y lo manda al Cpu    
    def test_manageAprocess(self):
        self.setUp_Scheduler_ByPriorities()
        self.planner.manageAProcess()
        self.assertEquals(self.cpu.currentProcess, self.process1)#El proceso que debe elegir es el proceso1
        

    
suite = unittest.TestLoader().loadTestsFromTestCase(TestPlanners)
unittest.TextTestRunner(verbosity=2).run(suite)